<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.mselect.js?"></script>
<script type="text/javascript">
$(function(){
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	var purchase_categories_data = <?=$purchase_categories?>;
	$(document).mselect(purchase_categories_data, 'product_categories_id','product_categories_id', "<?=isset($_GET['product_categories_id'])?$_GET['product_categories_id']:0?>");
	$("#download").click(function(){
		window.open("<?=url('DataCenter::Purchase/PurchaseSummaryReport', array('download' => true, 'is_search' => true))?>&product_categories_id=" + $("#product_categories_id").val()+"&warehouse_id=" + $(".warehouse_id").val());
	});
});
</script>
<fieldset>
<legend>查询条件</legend>
<form method="get">
<? include(Q::ini('custom_system/elements_dir') . 'searchform_element.php');?>
<table class="form_table">
<tr>
	<th>产品类别</th>
	<td colspan="3"><input type="hidden" name="product_categories_id" id="product_categories_id" /></td>
</tr>
<tr>
	<th>仓库</th>
	<td colspan="3">
		<select class="warehouse_id" name="warehouse_id">
		<?foreach ($warehouse as $w):?>
		<option value="<?=$w['id']?>" <?if (isset($_GET['warehouse_id']) && $_GET['warehouse_id'] == $w['id'])echo "selected='selected'"?>><?=$w['name']?></option>
		<?endforeach;?>
		</select>
		<input type="hidden" id="warehouse_id" value="<?=$_GET['warehouse_id']?>" />
	</td>
</tr>
<tr>
	<td><input type="hidden" name="is_search" value="1" /></td>
	<td><input type="submit" class="button" value="查询" /></td>
	<td><?if(isset($data)):?><a id="download" >下载报表</a><?endif;?></td>
</tr>
</table>
</form>
</fieldset>
<?if (!empty($_GET['is_search'])):?>
<table class="list_table" width="100%">
<tr>
	<th>产品类别</th>
	<th>SKU</th>
	<th>已欠货数</th>
	<th>预计差</th>
	<th>实际差</th>
	<th>待入库数</th>
</tr>

<?
$total_oos = $total_budget = $total_sku = $total_actual = $total_pending = 0;
foreach ($data as $d):
$sum_oos = $sum_budget = $sum_actual = $sum_pending = 0;
foreach ($d['products'] as $p)
{
	$sum_oos += $p['oos_quantity'];
	$sum_budget += $p['budget_quantity'];
	$sum_actual += $p['actual_quantity'];
	$sum_pending += $p['pending_quantity'];
}
$total_oos += $sum_oos;
$total_budget += $sum_budget;
$total_actual += $sum_actual;
$total_pending += $sum_pending;
$total_sku += count($d['products']);
?>
<tr>
	<td><?=$d['name']?></td>
	<td><?=count($d['products'])?></td>
	<td><?=$sum_oos;?></td>
	<td><?=$sum_budget;?></td>
	<td><?=$sum_actual;?></td>
	<td><?=$sum_pending;?></td>
</tr>
<?endforeach;?>
<tr>
	<td></td>
	<td><?=$total_sku;?></td>
	<td><?=$total_oos;?></td>
	<td><?=$total_budget;?></td>
	<td><?=$total_actual;?></td>
	<td><?=$total_pending?></td>
</tr>
</table>
<?endif;?>
<?php $this->_endblock();?>